package com.usabb.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.usabb.pojo.vo.AmenityAddVO;
import com.usabb.service.HospitalRelationService;
import com.usabb.service.RoundRelationService;
import org.apache.commons.lang.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.usabb.dao.AmenityRelationDao;
import com.usabb.domain.AmenityRelationDO;
import com.usabb.service.AmenityRelationService;
import com.ifast.common.base.CoreServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 
 * <pre>
 * 配套关联表
 * </pre>
 * <small> 2019-08-19 18:03:26 | Aron</small>
 */
@Service
public class AmenityRelationServiceImpl extends CoreServiceImpl<AmenityRelationDao, AmenityRelationDO> implements AmenityRelationService {

    @Autowired
    private RoundRelationService roundRelationService;
    @Autowired
    private HospitalRelationService hospitalRelationService;

    @Override
    public boolean insert(final Long bizId, final Long[] amenityIds, final String bizType) {
        if (ArrayUtils.isEmpty(amenityIds)) {
            log.info("Insert amenity relations. bizId:{}, amenityIds is empty,so return.", bizId);
            return true;
        }

        List<AmenityRelationDO> list = new ArrayList<>(amenityIds.length);
        for (Long amenityId : amenityIds) {
            AmenityRelationDO relationDO = new AmenityRelationDO();
            relationDO.setBizId(bizId);
            relationDO.setAmenityId(amenityId);
            relationDO.setBizType(bizType);

            list.add(relationDO);
        }

        return this.insertBatch(list);
    }

    @Override
    @Transactional
    public boolean saveOrUpdate(AmenityAddVO addVO) {
        final boolean amenityRelation = saveOrUpdateAmenityRelation(addVO);
        final boolean roundRelation = roundRelationService.saveOrUpdate(addVO);
        final boolean hospitalRelation = hospitalRelationService.saveOrUpdate(addVO);

        return hospitalRelation;
    }

    private boolean saveOrUpdateAmenityRelation(AmenityAddVO addVO) {
        final Long baseId = addVO.getBaseId();
        final String bizType = addVO.getBizType();
        deleteBy(bizType, baseId);

        return insert(baseId, addVO.getAmenityIds(), bizType);
    }

    @Override
    public List<AmenityRelationDO> getListBy(final String bizType, final Long bizId) {
        AmenityRelationDO amenityRelationDO = new AmenityRelationDO();
        amenityRelationDO.setBizId(bizId);
        amenityRelationDO.setBizType(bizType);
        Wrapper<AmenityRelationDO> wrapper = new EntityWrapper<>(amenityRelationDO);

        return this.selectList(wrapper);
    }

    @Override
    public boolean deleteBy(final String bizType, final Long bizId) {
        AmenityRelationDO param = new AmenityRelationDO();
        param.setBizId(bizId);
        param.setBizType(bizType);
        Wrapper<AmenityRelationDO> wrapper = new EntityWrapper<>(param);

        boolean delete = this.delete(wrapper);
        log.info("Deleted amenity relations.bizType:{}, bizId:{}, result:{}", bizType, bizId, delete);
        return delete;
    }

    @Override
    public List<Long> getAmenityIdListBy(final String bizType, final Long bizId) {
        List<AmenityRelationDO> relationDOS = getListBy(bizType, bizId);
        List<Long> ids = relationDOS.stream()
                .map(it -> it.getAmenityId())
                .collect(Collectors.toList());

        return ids;
    }


}
